package org.example.classloader;

import org.example.jar.MyClass;

import java.lang.reflect.Method;
import java.net.URL;

public class Main {

    public static void main(String[] args) throws Exception {
        //URL[] urls = {new URL("file:///path/to/jar1.jar"), new URL("file:///path/to/jar2.jar")};
        URL url=Main.class.getClassLoader().getResource("src/main/java/org/example/jar/MyClass.jar");
        CustomClassLoader customClassLoader = new CustomClassLoader(new URL[]{url});

        // 加载特定 jar 包中的类
        Class<?> myClass = customClassLoader.loadClass("org.example.jar.MyClass");

        // 反射调用该类的方法
        Object instance = myClass.newInstance();
        Method method = myClass.getMethod("greeting");
        System.out.println(method.invoke(instance));

        //强转之后调用
        System.out.println(((MyClass)instance).greeting());
    }
}
